/*
 * ModCpu.c
 *
 * Created: 22/03/2012 13:36:31
 *  Author: marionse
 */ 

#include "ModCpu.h"
#include <avr/io.h>
#include <avr/wdt.h>
#include <avr/interrupt.h>


/**
* \brief FONCTION PRIVEE DU MODULE
*/


/**
* \brief fonction d'initialisation du module
*/
Boolean ModCpuInit( void )
{
	Boolean lo_ret = TRUE;
	
	/** a faire uniquement en debug */
	/** enable JTAG */
#ifdef DEBUG	
	MCUCR &= ~(1U << JTD);
	MCUCR &= ~(1U << JTD);
#else
	MCUCR |= (1U << JTD);
	MCUCR |= (1U << JTD);
#endif
	
	/** start cpu by setting clock prescaler */
	CLKPR = (1U << CLKPCE);
	CLKPR = (0U & 0x0FU);
	
	/** start watchdog */
	wdt_reset();
	wdt_enable(0x7); // 256k -> 2sec
	
	/** disable all peripheral */
	PRR |= 0xFF;
	
	/** disable analog comparator */
	ACSR = (ACSR & ~(1 << ACIE)) | (1 << ACD);
	ACSR &= ~(1 << ACBG);
	
	/** enable global interrupt */
	sei();
	
	/** configure all io to input without pull-up */
	DDRA = 0;
	PORTA = 0;
	DDRB = 0;
	PORTB = 0;
	DDRC = 0;
	PORTC = 0;
	DDRD = 0;
	PORTD = 0;
	
	/** si on a un MOS pour couper l'alim de certain composant on active */
	#ifdef POWER_MOS_DDR
		POWER_MOS_PORT &= ~POWER_MOS_MASK;
		POWER_MOS_DDR |= POWER_MOS_MASK;	
	#endif // POWER_MOS_DDR
	
	return lo_ret;
}

/**
* \brief fonction appelee a chaque passage dans la boucle main
*/
Boolean ModCpuTick( void )
{
	Boolean lo_ret_code = TRUE;

	/** reset du watchdog */	
	wdt_reset();
	
	return lo_ret_code;
}

/**
* \brief effectue un reset du cpu
*/
void ModCpuReset( void )
{
	wdt_enable(WDTO_15MS);
	while(1)
		;
}
